﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace OnlineRailway.UI.Client
{
    public partial class Fare : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["RailwayOnlineConnectionString"].ToString());
        private SqlDataAdapter dap;
        private SqlCommand comm;
        private int ac,fc,sl;
        protected void Page_Load(object sender, EventArgs e)
        {
           LoadData();
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            int seatType = 0;
                if (ddlClass.SelectedValue == "AC")
                {
                    seatType = 50;
                }
                if (ddlClass.SelectedValue == "FC")
                {
                    seatType = 60;
                }
                else
                {
                    seatType = 70;
                }
            int total;
            comm = con.CreateCommand();
            comm.CommandText = "Select * from TrainSchedules where SCheduleID=@scheduleID";
            comm.Parameters.AddWithValue("@scheduleID", Request.QueryString["id"]);
            con.Open();
            SqlDataReader reader = comm.ExecuteReader();
            if (reader.Read())
            {
                int fare = (int)reader["Fare"];
                int seatNo = int.Parse(txtSeatNo.Text);
                //if (ddlClass.Text == "AC")
                //{
                //    total = (fare + 50) * seatNo;
                //    lblTotal.Text = "Total Fare: " + total.ToString();
                //}
                //if (ddlClass.Text == "FC")
                //{
                //    total = (fare + 60) * seatNo;
                //    lblTotal.Text = "Total Fare: " + total.ToString();
                //}
                //else
                //{
                //    total = (fare + 70) * seatNo;
                //    lblTotal.Text = "Total Fare: " + total.ToString();
                //}
                total = (fare + seatType) * seatNo;
                lblTotal.Text = "Total Fare: " + total.ToString();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            //comm.CommandText = "Select * from Seats s,DayMasters d where d.DayID=@day";
            //comm.Parameters.AddWithValue("@day",Request.QueryString["day"]);
            comm.CommandText = "Select * from Seats ";
            con.Open();
            SqlDataReader reader = comm.ExecuteReader();
            if (reader.Read())
            {
                ac = (int)reader["ACAvailable"];
                fc = (int)reader["FCAvailable"];
                sl = (int)reader["SLAvailable"];
            }
            con.Close();

            if (ddlClass.SelectedValue==("AC"))
            {
                comm.CommandText = "Update Seats set ACAvailable=@ac where DayID=@day";
                comm.Parameters.AddWithValue("@ac", ac - int.Parse(txtSeatNo.Text));
                //comm.Parameters.AddWithValue("@day", Request.QueryString["day"]);
                con.Open();
                comm.ExecuteNonQuery(); 
                con.Close();
            }
            if (ddlClass.SelectedValue == ("FC"))
            {
                comm.CommandText = "Update Seats set FCAvailable=@fc where DayID=@day";
                comm.Parameters.AddWithValue("@fc", fc - int.Parse(txtSeatNo.Text));
                //comm.Parameters.AddWithValue("@day", Request.QueryString["day"]);
                con.Open();
                comm.ExecuteNonQuery();
                con.Close();
            }
            else
            {
                comm.CommandText = "Update Seats set SLAvailable=@sl where DayID=@day";
                comm.Parameters.AddWithValue("@sl", sl - int.Parse(txtSeatNo.Text));
                //comm.Parameters.AddWithValue("@day", Request.QueryString["day"]);
                con.Open();
                comm.ExecuteNonQuery();
                con.Close();
            }
            
            LoadData();

        }

        private void LoadData()
        {
            comm = con.CreateCommand();
            comm.CommandText = "Select distinct ACAvailable as AC_AvailableSeat,FCAvailable as FC_AvailableSeat,SLAvailable as SL_AvailableSeat from Seats s,DayMasters d where s.DayID=d.DayID and d.SCheduleID=@scheduleId and d.DayID=@day";
            comm.Parameters.AddWithValue("@scheduleID", Request.QueryString["id"]);
            comm.Parameters.AddWithValue("@day", Request.QueryString["day"]);
            dap = new SqlDataAdapter(comm);
            con.Open();
            DataSet ds = new DataSet();
            dap.Fill(ds);
            DetailsView2.DataSource = ds;
            DetailsView2.DataBind();
            con.Close();
        }
    }
}